<template>
  <div :id="id" ref="boxwrap" :class="className" :style="{height:height,width:width}" />
</template>

<script>

import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '150px'
    },
    height: {
      type: String,
      default: '150px'
    }
  },
  data() {
    return {
      chart: null,
      fontColor: '#bebebe'
    }
  },
  mounted() {
    this.domResize()
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: {
          text: '线别占总产出比',
          left: '3%',
          top: '3%',
          textStyle: {
            fontSize: 16,
            color: this.fontColor
          }
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'veritical',
          top: '10%',
          right: '15%',
          textStyle: {
            fontSize: 16,
            color: this.fontColor
          }
        },
        series: [
          {
            name: '线别占总产出比',
            type: 'pie',
            radius: '50%',
            center: ['40%', '50%'],
            label: {
              position: 'inner',
              formatter: '{b}: {d}%'
            },
            data: [
              { value: 2000, name: 'A线' },
              { value: 3500, name: 'B线' }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    },
    domResize() {
      const that = this
      const dom = this.$refs.boxwrap
      this.$erd.listenTo(dom, function(element) {
        that.chart.resize()
      })
    }
  }
}
</script>
